package org.example.j2025_3_3.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.example.j2025_3_3.model.UserInfo;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        UserInfo userInfo = (UserInfo) session.getAttribute("USER_KEY");
        if(userInfo == null || userInfo.getName() == null){
             log.info("用户未登录");
            response.setStatus(401);
            return false;
        }
        return true ;
    }
}
